package com.bdqn.jk.service.impl;

import com.bdqn.jk.dao.AdminUserMapper;
import com.bdqn.jk.entity.AdminUser;
import com.bdqn.jk.service.AdminUserService;
import com.bdqn.jk.util.MD5Util;
import org.springframework.stereotype.Service;
import sun.security.provider.MD5;

import javax.annotation.Resource;

@Service
public class AdminUserServiceImpl implements AdminUserService {
    @Resource
    private AdminUserMapper adminUserMapper;

    //登录
    @Override
    public AdminUser login(String userName, String password) {
        return adminUserMapper.login(userName,password);
    }

    //根据ID查询信息
    @Override
    public AdminUser selectById(Integer loginUserId) {
        return adminUserMapper.selectById(loginUserId);
    }

    //修改密码
    @Override
    public Boolean updatePassword(Integer loginUserId, String originalPassword, String newPassword) {
        AdminUser adminUser =adminUserMapper.selectById(loginUserId);
        if(adminUser!=null){
            //老密码
            String passwordMD5 = MD5Util.MD5Encode(originalPassword, "utf-8");
            //新密码
            String newpasswordMD5 = MD5Util.MD5Encode(newPassword, "utf-8");
            //新老密码是否相等
            if(passwordMD5.equals(adminUser.getLoginPassword())){
                adminUser.setLoginPassword(newpasswordMD5);
                if(adminUserMapper.updateall(adminUser)>0){
                    return true;
                }
            }
        }
        return false;
    }

    //修改登录用户名
    @Override
    public Boolean updateName(Integer loginUserId, String loginUserName, String nickName) {
        AdminUser adminUser = adminUserMapper.selectById(loginUserId);
        if(adminUser != null){
            //设置新名称
            adminUser.setLoginUserName(loginUserName);
            adminUser.setNickName(nickName);
            if(adminUserMapper.updateall(adminUser)>0){
                return true;
            }
        }
        return false;
    }


}
